<?php

namespace Hilaoyu\LaravelExtend\Services\Auth\Traits;


use Carbon\Carbon;
use Hilaoyu\LaravelExtend\Services\Auth\AuthService;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

trait ModelWithLoginRefreshTrait
{


    protected function fieldNameLoginTime():string {
        return 'login_time';
    }

    protected function fieldNamePlatform():string {
        return 'platform';
    }
    protected function fieldNameSource():string {
        return 'source';
    }
    protected function fieldNameClientIp():string {
        return 'client_ip';
    }

    protected function fieldNameExpireTime():string {
        return 'expire_time';
    }


    public function fillExpireTime($expire_time = '')
    {
        if(! $this->fieldNameExpireTime()){
            return $this;
        }

        $expire_time = trim($expire_time);
        if (empty($expire_time)) {
            $expire = 60 * 60;
            if ($this->exists) {
                $old_expire = intval(strtotime($this->expire_time) - strtotime($this->updated_at));
                if ($old_expire > $expire) {
                    $expire = $old_expire;
                }
            }

            $expire_time = date('Y-m-d H:i:s', (time() + $expire));

        }
        $this->{$this->fieldNameExpireTime()} = $expire_time;

        return $this;
    }

    public function fillPlatform($platform = '')
    {
        if(! $this->fieldNamePlatform()){
            return $this;
        }

        $platform = trim($platform);
        if (empty($platform)) {
            $platform = trim(request()->getClientPlatform());
        }

        $this->{$this->fieldNamePlatform()} = $platform;

        return $this;
    }

    public function fillSource($source = '')
    {
        if(! $this->fieldNameSource()){
            return $this;
        }
        $source = trim($source);
        if (empty($source)) {
            $source = trim(request()->getClientSource());
        }

        $this->{$this->fieldNameSource()} = $source;

        return $this;
    }
    public function fillClientIp($ip = '')
    {
        if(! $this->fieldNameClientIp()){
            return $this;
        }
        $ip = trim($ip);
        if (empty($ip)) {
            $ip = trim(request()->ip());
        }

        $this->{$this->fieldNameClientIp()} = $ip;

        return $this;
    }

    public function fillLoginTime($time = '')
    {
        if(! $this->fieldNameLoginTime()){
            return $this;
        }

        $time = trim($time);
        if (empty($time)) {
            $time = Carbon::now()->toDateTimeString();
        }

        $this->{$this->fieldNameLoginTime()} = $time;

        return $this;
    }

    public function loginRefresh()
    {
        UtilSupport::tryQuiet(function () {
            $this->fillExpireTime();
        });
        UtilSupport::tryQuiet(function () {
            $this->fillPlatform();
        });
        UtilSupport::tryQuiet(function () {
            $this->fillSource();
        });
        UtilSupport::tryQuiet(function () {
            $this->fillClientIp();
        });
        UtilSupport::tryQuiet(function () {
            $this->fillLoginTime();
        });
        UtilSupport::tryQuiet(function () {
            $this->save();
        });
        // 同时更新缓存Token信息
        if (Str::contains($this->getTable(),'token')){
            $key = AuthService::GetTokenCahceKeyPrefix().$this->getKey();
            if ($expire_time = data_get($this,'expire_time')){
                Cache::put($key, $this, Carbon::parse($expire_time));
            }
        }
        return $this;
    }
}
